﻿using System;
using System.Collections.Generic;

namespace Fuse8.DomainFramework.DataAccess.SqlDatabase
{
    /// <summary>
    /// 
    /// </summary>
    public class DefaultEntityMappingConfiguratorWithChain : IEntityMappingConfiguratorChain
    {
        private List<IEntityMappingConfigurator> chain = new List<IEntityMappingConfigurator>();
        private IEntityMappingConfigurator defaultConfigurator;

        /// <summary>
        /// Returns table name by entity type
        /// </summary>
        /// <param name="type">Entity type</param>
        /// <returns></returns>
        public string GetEntityTableName(Type type)
        {
            foreach (var configurator in chain)
            {
                var result = configurator.GetEntityTableName(type);
                if (result != null)
                {
                    return result;
                }
            }

            return defaultConfigurator.GetEntityTableName(type);
        }

        /// <summary>
        /// Returns entity column name by entity type and sql column name
        /// </summary>
        /// <param name="type">Entity type</param>
        /// <param name="column">Sql column name</param>
        /// <returns></returns>
        public string GetEntityColumnAlias(Type type, string column)
        {
            foreach (var configurator in chain)
            {
                var result = configurator.GetEntityColumnAlias(type, column);
                if (result != null)
                {
                    return result;
                }
            }

            return defaultConfigurator.GetEntityColumnAlias(type, column);
        }

        /// <summary>
        /// Returns collection of the ignored columns by entity type
        /// </summary>
        /// <param name="type">Entity type</param>
        /// <returns></returns>
        public IEnumerable<string> GetIgnoredColumns(Type type)
        {
            foreach (var configurator in chain)
            {
                var result = configurator.GetIgnoredColumns(type);
                if (result != null)
                {
                    return result;
                }
            }

            return defaultConfigurator.GetIgnoredColumns(type);
        }

        /// <summary>
        /// Gest database related name for property
        /// </summary>
        /// <param name="type">Type where specified prorpety situated</param>
        /// <param name="property">Property name</param>
        /// <returns></returns>
        public string GetRelatedPropertyColumnName(Type type, string property)
        {
            foreach (var configurator in chain)
            {
                var result = configurator.GetRelatedPropertyColumnName(type, property);
                if (result != null)
                {
                    return result;
                }
            }

            return defaultConfigurator.GetRelatedPropertyColumnName(type, property);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="configurator"></param>
        public void Add(IEntityMappingConfigurator configurator)
        {
            chain.Add(configurator);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="configurator"></param>
        public void AddDefault(IEntityMappingConfigurator configurator)
        {
            defaultConfigurator = configurator;
        }
    }
}